import {
	serveraddress
} from './serverUrl'

function request(controller, action, method = "POST", data, callback) {
	uni.request({
		url: serveraddress + "/" + controller + "/" + action,
		method: method,
		sslVerify: false,
		header: {
			'Content-Type': 'application/x-www-form-urlencoded',
		},
		data: data,
		dataType: 'json',
		success: function(res) {
			//console.log(res);
			if (res.statusCode == 200) {
				
				//判断返回的数据格式
				if (res.data.error == 0) {
					return callback({
						"status": 1,
						"data": res.data
					});
				} else if (res.data.error > 0) {
					//失败 接口返回错误信息提醒
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
					return callback({
						"status": 1,
						"data": res.data
					})
				} else if (res.data.error == -1) {
					//登录失败，token失效
					uni.showModal({
						content: '登录失效，重新登录？',
						success: e => {
							if (e.confirm) {
								setTimeout(() => {
									uni.removeStorageSync('token');
									uni.removeStorageSync('userInfo');
									uni.navigateTo({
										url: '/pages/public/login'
									});
								}, 200);
							}
						}
					});
					return callback({
						"status": 0,
						"data": res.data
					})
				} else if (res.data.error == -2) {
					//未实名认证
					uni.showModal({
						content: '去实名认证？',
						success: e => {
							if (e.confirm) {
								setTimeout(() => {
									uni.navigateTo({
										url: '/pages/user/verified'
									});
								}, 200);
							}
						}
					});
					return callback({
						"status": 0,
						"data": res.data
					});
				}else{
					uni.showToast({
						title: '返回数据异常',
						icon: 'none'
					})
					return callback({
						"status": 0,
						"data": res.data
					})
				}

			} else {
				uni.showToast({
					title: '服务器状态错误',
					icon: 'none'
				})
				return callback({
					"status": 0,
					"data": res.data
				})
			}
		},
		fail: function(res) {
			uni.showToast({
				title: '连接服务器失败',
				icon: 'none'
			})
			return callback({
				"status": 0
			})
		}
	})

}



function uploadFile(file, callback) {
	uni.uploadFile({
		url: serveraddress + "/api/files/Upload",
		filePath: file,
		name: "file",
		header: {
			'content-type': 'multipart/form-data'
		},
		success: function(res) {
			return typeof callback == "function" && callback(res.data.replace(/\"/g, ''))
		},
		fail: function(res) {
			return typeof callback == "function" && callback(null)
		}
	})
}

// 上传一组文件
function uploadfile(images, index, resarr, callback) {
	uni.uploadFile({
		url: serveraddress + '/api/files/Upload',
		filePath: images[index],
		name: 'file',
		header: {
			'content-type': 'multipart/form-data'
		},
		success: function(res) {
			console.log(res)
			if (res.statusCode == 200) {
				resarr.push(res.data.replace(/\"/g, ''))
				if (images.length == index + 1) {
					return callback({
						"res": 1,
						"data": resarr
					})
				}
				uploadfile(images, index + 1, resarr, callback)
			} else {
				return callback({
					"res": 0
				})
			}
		},
		fail: function(res) {
			return callback({
				"res": 0
			})
		}
	})
}

function uploadfiles(images, callback) {
	console.log(images)
	uploadfile(images, 0, [], callback);
}

export default {
	request: request,
	uploadFile: uploadFile,
	uploadfiles: uploadfiles
}
